﻿using Newtonsoft.Json;
using PMIS.Common;
using PMIS.Dialogs;
using PMIS.HttpRequest;
using PMIS.Shared.DTO;
using PMIS.Shared.Models;
using PMIS.Views.Menu;
using System.Windows.Input;

namespace PMIS.ViewModels.Menu
{
    public class MenuViewModel : ViewModelBase
    {
        private readonly IDialogService _dialogService;
        private readonly HttpService _httpService;
        public MenuViewModel(IDialogService dialogService, HttpService httpService)
        {
            _dialogService = dialogService;
            _httpService = httpService;

            LoadNavMenu();
        }

        private void OnAdd()
        {
            _dialogService.ShowDialog(nameof(AddEditMenu), null, AddEditCallBack);
        }

        private void OnDelete()
        {

        }

        private void OnEdit(object obj)
        {
            if (obj is MenuDto menu)
            {
                DialogParameters para = new DialogParameters();
                para.Add("data", menu.DeepClone());
                _dialogService.ShowDialog(nameof(AddEditMenu), para, AddEditCallBack);
            }
        }

        private void OnPrint()
        {

        }

        private void OnRefresh()
        {

        }

        private ICommand _addCommand;
        public ICommand AddCommand => _addCommand ??= new DelegateCommand(OnAdd);

        private ICommand _editCommand;
        public ICommand EditCommand => _editCommand ??= new DelegateCommand<object>(obj => OnEdit(obj));

        private ICommand _deleteCommand;
        public ICommand DeleteCommand => _deleteCommand ??= new DelegateCommand(OnDelete);

        private ICommand _printCommand;
        public ICommand PrintCommand => _printCommand ??= new DelegateCommand(OnPrint);

        private ICommand _refreshCommand;
        public ICommand RefreshCommand => _refreshCommand ??= new DelegateCommand(OnRefresh);

        private void AddEditCallBack(IDialogResult dialogResult)
        {
            if (dialogResult != null && dialogResult.Result == ButtonResult.OK)
            {
                LoadNavMenu();
            }
        }

        private List<MenuDto>? _allMenuList;
        public List<MenuDto>? AllMenuList
        {
            get
            {
                return _allMenuList;
            }
            set
            {
                SetProperty(ref _allMenuList, value);
            }
        }

        private List<MenuDto>? _treeMenuList;
        public List<MenuDto>? TreeMenuList
        {
            get
            {
                return _treeMenuList;
            }
            set
            {
                SetProperty(ref _treeMenuList, value);
            }
        }

        private List<DataDictDto> _dataDictList;

        private async void LoadNavMenu()
        {
            var response = await _httpService.GetAsync(Api.MENU_GET_LIST);
            if (response?.code == 1)
            {
                var data = JsonConvert.DeserializeObject<PagingModel<MenuDto>>(JsonConvert.SerializeObject(response.data));
                AllMenuList = data.Items.OrderBy(p => p.Sort).ToList();

                await LoadDataDict();

                foreach (var item in AllMenuList)
                {
                    foreach (var dataDict in _dataDictList)
                    {
                        if (item.Type == dataDict.DictKey)
                        {
                            item.TypeName = dataDict.DictName;
                        }
                    }
                }

                LoadTreeMenuList();
            }
        }

        private async Task LoadDataDict()
        {
            var parameters = new Dictionary<string, object>();
            parameters["dictType"] = "MenuType";
            var response = await _httpService.GetAsync(Api.DATADICT_GET_LIST, parameters);
            if (response?.code == 1)
            {
                var data = JsonConvert.DeserializeObject<PagingModel<DataDictDto>>(JsonConvert.SerializeObject(response.data));
                _dataDictList = data.Items;
            }
        }

        private void LoadTreeMenuList()
        {
            var allMenus = AllMenuList;
            TreeMenuList = new List<MenuDto>(allMenus.Where(x => x.ParentId == null));
            foreach (var item in TreeMenuList)
            {
                item.IsExpanded = true;
                LoadChildMenuList(item);
            }
        }

        public void LoadChildMenuList(MenuDto menu)
        {
            var allMenus = AllMenuList;
            menu.Children = allMenus.Where(x => x.ParentId == menu.Id).ToList();
            foreach (var subitem in menu.Children)
            {
                // Recursive call
                LoadChildMenuList(subitem);
            }
        }
    }
}
